home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_01_05 / 1n05039a < prev    next >
Text File  |  1990-08-24  |  8KB  |  230 lines

  1. Figure 3.
  2.  
  3. /******************************************************************************
  4.  * LISTBOX.C
  5.  */
  6.  
  7. #define LINT_ARGS
  8.  
  9. #include <stdio.h>
  10. #include <windows.h>
  11. #include "listbox.h"
  12.  
  13. HANDLE    hInst;                        /* Global Instance Handle           */
  14.  
  15. long FAR PASCAL MainFormWndProc(HWND, unsigned, WORD, LONG);
  16. BOOL FAR PASCAL CustomerMaintenance(HWND, unsigned, WORD, LONG);
  17. BOOL FAR PASCAL CustomerEdit(HWND, unsigned, WORD, LONG);
  18.  
  19. char Account[7];
  20. char Name[40];
  21.  
  22. /*****************************************************************************
  23.  * WinMain()
  24.  */
  25. int PASCAL WinMain(hInstance, hPrevInstance, CmdLine, CmdShow)
  26.     HANDLE hInstance,hPrevInstance;
  27.     LPSTR CmdLine;
  28.     int CmdShow;
  29.     {
  30.     WNDCLASS WndClass;
  31.     HWND hWnd;
  32.     MSG msg;
  33.  
  34.     hInst=hInstance;                             /* Store Instance handle   */
  35.  
  36.     /*
  37.      * Register Main Form Window Class
  38.      */
  39.     if (!hPrevInstance){
  40.        WNDCLASS WndClass;
  41.        WndClass.lpszClassName = (LPSTR)"techspec5";
  42.        WndClass.hInstance     = hInstance;
  43.        WndClass.lpfnWndProc   = MainFormWndProc;
  44.        WndClass.style         = CS_HREDRAW | CS_VREDRAW;
  45.        WndClass.hbrBackground = GetStockObject(WHITE_BRUSH);
  46.        WndClass.hIcon         = NULL;
  47.        WndClass.lpszMenuName  = "MAINMENU";
  48.        WndClass.hCursor       = LoadCursor(NULL,IDC_ARROW);
  49.        WndClass.cbClsExtra    = NULL;
  50.        WndClass.cbWndExtra    = NULL;
  51.        if (!RegisterClass(&WndClass))
  52.           return(NULL);
  53.        }
  54.  
  55.     /*
  56.      * Create MainForm Window
  57.      */
  58.     hWnd = CreateWindow("techspec5",         /* window class name       */
  59.                         "Listbox Example Program", /* window caption    */
  60.                         WS_OVERLAPPEDWINDOW, /* window style            */
  61.                         0,                   /* initial x position      */
  62.                         0,                   /* initial y position      */
  63.                         CW_USEDEFAULT,       /* initial x size          */
  64.                         0,                   /* initial y size          */
  65.                         NULL,                /* parent window handle    */
  66.                         NULL,                /* window menu handle      */
  67.                         hInstance,           /* program instance handle */
  68.                         NULL);               /* create parameters       */
  69.     if (!hWnd)
  70.       return (NULL);
  71.     /*
  72.      * Message Loop
  73.      */
  74.     while (GetMessage(&msg,NULL,0,0)){   
  75.         TranslateMessage(&msg);
  76.         DispatchMessage(&msg);
  77.         }
  78.     return(msg.wParam);
  79.     }
  80. /****************************************************************************
  81.  * MainFormWndProc()
  82.  */
  83. long FAR PASCAL MainFormWndProc(hWnd, message, wParam, lParam)
  84.   HWND hWnd;
  85.   unsigned message;
  86.   WORD wParam;
  87.   LONG lParam;
  88.   {
  89.   switch (message){
  90.      case WM_CREATE:
  91.           ShowWindow(hWnd,SW_SHOWNORMAL);
  92.           UpdateWindow(hWnd);
  93.           break;
  94.      case WM_COMMAND:
  95.           switch(wParam){
  96.              case IDM_CUSTMAINT:
  97.                   DoDialog(hInst,hWnd,"CUSTOMERMAINTENANCE",CustomerMaintenance);
  98.                   break;
  99.              case IDM_EXIT:
  100.                   DestroyWindow(hWnd);
  101.                   break;
  102.              }
  103.           break;
  104.      case WM_DESTROY:
  105.           PostQuitMessage(0);
  106.           break;
  107.      default:return(DefWindowProc(hWnd,message,wParam,lParam));
  108.      }
  109.   return (NULL);
  110.   }
  111. /****************************************************************************
  112.  * DoDialog
  113.  */
  114. int DoDialog(HANDLE hInst,HWND hWnd, char *Resource, FARPROC lpProc){
  115.    FARPROC lpfn;
  116.    BOOL ret;
  117.    lpfn = MakeProcInstance(lpProc, hInst);
  118.    ret = DialogBox(hInst, Resource, hWnd, lpfn);
  119.    FreeProcInstance(lpfn);
  120.    return(ret);
  121.    }
  122. /****************************************************************************
  123.  * CustomerMaintenance()
  124.  */                            
  125. BOOL FAR PASCAL CustomerMaintenance(HWND hDlg,unsigned message,WORD wParam,LONG lParam){
  126.   static char buf[100];
  127.   int i,y;
  128.   long ListItem;
  129.   switch (message){
  130.      case WM_INITDIALOG:
  131.           y=ID_NAME;
  132.           for(i=ID_ACCOUNT;i<=ID_ACCOUNT+4;i++){
  133.               LoadString(hInst,i,Account,6);
  134.               LoadString(hInst,y++,Name,39);
  135.               sprintf(buf,"%-6.6s %-s",Account,Name);
  136.               SendMessage(GetDlgItem(hDlg,ID_LISTBOX),LB_ADDSTRING,0,(LONG)(LPSTR)buf);
  137.               }
  138.           SendMessage(GetDlgItem(hDlg,ID_LISTBOX),LB_SETCURSEL,0,0L);
  139.           return(FALSE);
  140.      case WM_CLOSE:
  141.           SendMessage(hDlg,WM_COMMAND,ID_OK,0L); 
  142.           break;
  143.      case WM_COMMAND:
  144.           switch(wParam){
  145.              case ID_DELETE:
  146.                   ListItem=SendMessage(GetDlgItem(hDlg,ID_LISTBOX),LB_GETCURSEL,0,0L);
  147.                   if(ListItem==-1L)
  148.                      break;
  149.                   SendMessage(GetDlgItem(hDlg,ID_LISTBOX),LB_GETTEXT,(WORD)ListItem,(LONG)(LPSTR)buf);
  150.                   sprintf(Account,"%-s",buf);  
  151.                   sprintf(buf,"Delete Account# %-s ?",Account);
  152.                   MessageBeep(0);
  153.                   if (MessageBox(hDlg,buf,"Confirm",MB_OKCANCEL|MB_ICONQUESTION)==IDCANCEL)
  154.                       break;
  155.                   SendMessage(GetDlgItem(hDlg,ID_LISTBOX),LB_DELETESTRING,(WORD)ListItem,0L);
  156.                   SendMessage(GetDlgItem(hDlg,ID_LISTBOX),LB_SETCURSEL,(WORD)max(ListItem-1L,0L),0L);
  157.                   break;
  158.              case ID_ADD:
  159.                   Account[0]=NULL;
  160.                   if (DoDialog(hInst,hDlg,"CUSTOMEREDIT",CustomerEdit)){
  161.                      sprintf(buf,"%-6.6s %-s",Account,Name);
  162.                      SendMessage(GetDlgItem(hDlg,ID_LISTBOX),LB_ADDSTRING,0,(LONG)(LPSTR)buf);
  163.                      }
  164.                   break;
  165.              case ID_LISTBOX:
  166.                   if (HIWORD(lParam)!=2)
  167.                      break;
  168.              case ID_EDIT:
  169.                   ListItem=SendMessage(GetDlgItem(hDlg,ID_LISTBOX),LB_GETCURSEL,0,0L);
  170.                   if(ListItem==-1L)
  171.                      break;
  172.                   SendMessage(GetDlgItem(hDlg,ID_LISTBOX),LB_GETTEXT,(WORD)ListItem,(LONG)(LPSTR)buf);
  173.                   sprintf(Account,"%-6.6s",buf);  
  174.                   sprintf(Name,"%-s",buf+7);  
  175.                   if (DoDialog(hInst,hDlg,"CUSTOMEREDIT",CustomerEdit)){
  176.                         sprintf(buf,"%-6.6s %-s",Account,Name);
  177.                         SendMessage(GetDlgItem(hDlg,ID_LISTBOX),WM_SETREDRAW,FALSE,0L);
  178.                         SendMessage(GetDlgItem(hDlg,ID_LISTBOX),LB_DELETESTRING,(WORD)ListItem,0L);
  179.                         SendMessage(GetDlgItem(hDlg,ID_LISTBOX),WM_SETREDRAW,TRUE,0L);
  180.                         SendMessage(GetDlgItem(hDlg,ID_LISTBOX),LB_INSERTSTRING,(WORD)ListItem,(LONG)(LPSTR)buf);
  181.                         }
  182.                   SendMessage(GetDlgItem(hDlg,ID_LISTBOX),LB_SETCURSEL,(WORD)ListItem,0L);
  183.                   break;
  184.              case ID_OK:
  185.                   EndDialog(hDlg,TRUE);
  186.                   break;
  187.              default:return(FALSE);
  188.              }
  189.           break;
  190.      default:return(FALSE);
  191.      }
  192.   return(TRUE);
  193.   }
  194. /****************************************************************************
  195.  * CustomerEdit()
  196.  */                            
  197. BOOL FAR PASCAL CustomerEdit(HWND hDlg,unsigned message,WORD wParam,LONG lParam){
  198.   switch (message){
  199.      case WM_INITDIALOG:
  200.           if (Account[0]!=NULL){
  201.             SetWindowText(hDlg,"Edit Customer");
  202.             SetDlgItemText(hDlg,ID_ACCOUNT,Account);
  203.             SetDlgItemText(hDlg,ID_NAME,Name);
  204.             EnableWindow(GetDlgItem(hDlg,ID_ACCOUNT),FALSE);
  205.             }
  206.           else SetWindowText(hDlg,"Add Customer");
  207.           return(FALSE);
  208.      case WM_CLOSE:
  209.           SendMessage(hDlg,WM_COMMAND,ID_CANCEL,0L);
  210.           break;
  211.      case WM_COMMAND:
  212.           switch(wParam){
  213.              case ID_OK:
  214.                   GetDlgItemText(hDlg,ID_ACCOUNT,Account,7);
  215.                   GetDlgItemText(hDlg,ID_NAME,Name,21);
  216.